package org.jeecg.modules.demo.food.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.checkerframework.checker.units.qual.m;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.compages.entity.MaterialCompages;
import org.jeecg.modules.demo.compages.mapper.MaterialCompagesMapper;
import org.jeecg.modules.demo.compages.service.IMaterialCompagesService;
import org.jeecg.modules.demo.food.entity.Material;
import org.jeecg.modules.demo.food.mapper.MaterialMapper;
import org.jeecg.modules.demo.food.service.IMaterialService;
import org.jeecg.tools.MyTestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectList;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


/**
 * @Description: material
 * @Author: jeecg-boot
 * @Date:   2020-04-09
 * @Version: V1.0
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements IMaterialService {

	@Resource
	private MaterialMapper materialMapper;

	@Autowired
	private IMaterialCompagesService materialCompagesService;

	@Override
	public int upDeleteFLag(List list) {
		// TODO Auto-generated method stub
		return materialMapper.upDeleteFLag(list);
	}

	@Transactional
	@Override
	public int insertCompages(Material material,List<Material> materials) {
		// TODO Auto-generated method stub

		Iterator<Material> materialsiter=materials.iterator();

		boolean b=false;

		int id=0;
		id=materialMapper.saveCompages(material);

		if(id==0)
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

		int autoId=material.getId();

		materialsiter=materials.iterator();
		while (materialsiter.hasNext()) {
			Material material1=materialsiter.next();
			MaterialCompages materialCompages = new MaterialCompages();
			materialCompages.setMaterialCompagesId(autoId);
			materialCompages.setMaterialId(material1.getId());
			b=materialCompagesService.save(materialCompages);
			if(!b)
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}

		if(b)
			return 1;
		else
			return 0;
	}

	@Transactional
	@Override
	public int sale(List<String> materials,BigDecimal bCount) {
		// TODO Auto-generated method stub
		Iterator<String> materialsiter0=materials.iterator();

		List<Integer> aList=new ArrayList();

		while (materialsiter0.hasNext()) {
			int id=Integer.parseInt(materialsiter0.next());
			Material material=getById(id);
			BigDecimal storage=material.getStorage();

			aList.add(material.getId());

			if(storage.floatValue()<bCount.floatValue()){
				return -2;
			}


		}

		Iterator<String> materialsiter=materials.iterator();

		boolean b=false;

		while (materialsiter.hasNext()) {

			int id=Integer.parseInt(materialsiter.next());

			Material material=getById(id);

			System.out.print("material==");
			MyTestUtil.print(material);

			BigDecimal storage=material.getStorage();

			if(storage.floatValue()<bCount.floatValue()){
				try{
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				}catch (TransactionException e) {
					// TODO: handle exception
				}
				return -2;
			}

			storage=storage.subtract(bCount);

			Material material1=new Material();

			material1.setId(id);
			material.setStorage(storage);

			b=this.updateById(material);

			if(!b)
				try{
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				}catch (Exception e) {
					// TODO: handle exception
				}

			if(material.getCombination()==1){

				List list2=materialCompagesService.select(aList);

				Iterator iterator=list2.iterator();

				while(iterator.hasNext()){

					MaterialCompages materialCompages=(MaterialCompages) iterator.next();
					System.out.println("mmmmmmmmmmmmmmmmmm");
					MyTestUtil.print(materialCompages);
					int id2=materialCompages.getMaterialId();

					Material material2=getById(id2);

					BigDecimal storage2=material2.getStorage();

					if(storage2.floatValue()<bCount.floatValue()){
						try{
							TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						}catch (TransactionException e) {
							// TODO: handle exception
						}
						return -2;
					}

					storage2=storage2.subtract(bCount);

					Material material3=new Material();

					material3.setId(id2);
					material3.setStorage(storage2);

					b=this.updateById(material3);

					if(!b){
						try{
							TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						}catch (Exception e) {
							// TODO: handle exception
						}

						return -2;
					}
				}

			}

		}

		if(b)
			return 1;
		else
			return 0;
	}

	@Override
	public List<Material> select(List<Integer> list) {
		// TODO Auto-generated method stub
		return materialMapper.select(list);
	}



}
